
/*    This file is part of the Managarm operating system.
 *   Copyright (C) 2007, 2008, 2009  Alexander van der Grinten
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

package org.managarm.jmanavalib.utils;

// import java util classes
import java.util.HashMap;
import java.util.Iterator;

import org.managarm.jmanavalib.core.VaClass;
import org.managarm.jmanavalib.core.VaClsInfo;
import org.managarm.jmanavalib.core.VaMthInfo;
import org.managarm.jmanavalib.core.VaObject;
import org.managarm.jmanavalib.core.VaProcedure;

// import managarm virtual architecture classes

public final class VaImage extends VaObject {
	private static final class ClsIterator implements Iterator<VaClsInfo> {
		private final Iterator<VaClsInfo> iterator;

		public ClsIterator(Iterator<VaClsInfo> iterator) {
			this.iterator = iterator;
		}

		// see Iterator for a detailed description
		public final boolean hasNext() {
			return iterator.hasNext();
		}
		
		// see Iterator for a detailed description
		public final VaClsInfo next() {
			return iterator.next();
		}

		// see Iterator for a detailed description
		public final void remove() {
			throw new UnsupportedOperationException();
		}
	}
	
	private static final class MthIterator implements Iterator<VaMthInfo> {
		private final Iterator<VaMthInfo> iterator;

		public MthIterator(Iterator<VaMthInfo> iterator) {
			this.iterator = iterator;
		}

		// see Iterator for a detailed description
		public final boolean hasNext() {
			return iterator.hasNext();
		}
		
		// see Iterator for a detailed description
		public final VaMthInfo next() {
			return iterator.next();
		}

		// see Iterator for a detailed description
		public final void remove() {
			throw new UnsupportedOperationException();
		}
	}
	
	// maps methods to procedures
	private final HashMap<VaMthInfo, VaProcedure> methodMap
			= new HashMap<VaMthInfo, VaProcedure>();
	
	// maps class descriptions to classes
	private final HashMap<VaClsInfo, VaClass> classMap
			= new HashMap<VaClsInfo, VaClass>();
	
	// adds one class body to this image
	public final void addClass(VaClass cls) {
		classMap.put(cls.clsInfo, cls);
	}
	
	// adds one method body to this image
	public final void addMethod(VaMthInfo method,
			VaProcedure procedure) {
		methodMap.put(method, procedure);
	}
	
	// returns the body of class "cls"
	public final VaClass getClass(VaClsInfo cls) {
		return classMap.get(cls);
	}
	
	// returns the body of method "mth"
	public final VaProcedure getMethod(VaMthInfo mth) {
		return methodMap.get(mth);
	}
	
	// returns an iterator that iterates all classes
	public final Iterator<VaClsInfo> classIterator() {
		return new ClsIterator(classMap.keySet().iterator());
	}
	
	// returns an iterator that iterates all methods
	public final Iterator<VaMthInfo> methodIterator() {
		return new MthIterator(methodMap.keySet().iterator());
	}
}
